package com.ct.gd.passport.view.travel;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.domain.ExchangeRateInfo;
import com.ct.gd.passport.view.AbsCatalogFragment;
import com.up91.common.android.content.SimpleLoader;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public class ExchangeRateInfoFragment extends AbsCatalogFragment<ExchangeRateInfo>{
	@Override
	protected SimpleListAdapter<ExchangeRateInfo> onInitListAdapter() {
		return new ExchangeRateInfoAdapter(getActivity(), null);
	}
	
	private static class ExchangeRateInfoAdapter extends SimpleListAdapter<ExchangeRateInfo> {
		private class ViewHolder {
			TextView tvLhs;
			TextView tvTrans;
			TextView tvRhs;
		}

		public ExchangeRateInfoAdapter(Context context, List<ExchangeRateInfo> data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.exchange_rate_info_item, null);
				holder = new ViewHolder();
				holder.tvLhs = (TextView) convertView.findViewById(R.id.tv_exchange_rate_info_lhs);
				holder.tvTrans = (TextView) convertView.findViewById(R.id.tv_exchange_rate_info_trans);
				holder.tvRhs = (TextView) convertView.findViewById(R.id.tv_exchange_rate_info_rhs);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ExchangeRateInfo t = getData().get(position);
			holder.tvLhs.setText(t.getLhs());
			holder.tvTrans.setText(t.getError());
			holder.tvRhs.setText( t.getRhs());
			return convertView;
		}
	}
	
	@Override
	protected void beforeLoad(Loader<List<ExchangeRateInfo>> loader) {
	}

	@Override
	public Loader<List<ExchangeRateInfo>> onCreateLoader(int id, Bundle args) {
		return new ExchangeRateInfoLoader(getActivity(), getListView());
	}
	
	private static class ExchangeRateInfoLoader extends SimpleLoader<List<ExchangeRateInfo>> {
		public ExchangeRateInfoLoader(Context context, View view) {
			super(context, view);
		}
		@Override
		protected List<ExchangeRateInfo> loadData() throws Exception {
			return ExchangeRateInfo.load();
		}
	}
}
